//
//  XMAddressAddView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/29.
//

#import "XMAddressAddView.h"

@interface XMAddressAddView ()

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *contentView;

@property (nonatomic, strong) UITextField *nameTextField;
@property (nonatomic, strong) UITextField *phoneTextField;
@property (nonatomic, strong) UITextField *addressTextField;

@property (nonatomic, strong) UIButton *placeSelectBtn;

@property (nonatomic, strong) UISwitch *defaultSwitch;

@property (nonatomic, strong) XMAddressModel *addressModel;

@property (nonatomic, copy) NSString *areaStr;
@property (nonatomic, copy) NSString *areaId;

@end

@implementation XMAddressAddView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.bgView];
    [self addSubview:self.contentView];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(200.0) + KScaleWidth(10.0))];
    bgView.backgroundColor = kColorHEX(0xffffff);
    [_contentView addSubview:bgView];
    NSArray *titleArr = @[@"姓名", @"手机号码", @"所在地区", @"详细地址"];
    for (int i = 0; i < titleArr.count; i ++) {
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(25.0), KScaleWidth(10.0) + KScaleWidth(50.0)*i, KScaleWidth(70.0), KScaleWidth(50.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
        titleLabel.text = titleArr[i];
        [bgView addSubview:titleLabel];
        if (i < titleArr.count) {
            UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(KScaleWidth(12.0), titleLabel.maxY - 1, self.width - KScaleWidth(12.0)*2, 1)];
            lineView.backgroundColor = kColorHEX(0xE7E7E7);
            [bgView addSubview:lineView];
        }
    }
    [bgView addSubview:self.nameTextField];
    [bgView addSubview:self.phoneTextField];
    [bgView addSubview:self.placeSelectBtn];      
    [bgView addSubview:self.addressTextField];
    
    UIView *defaultBgView = [[UIView alloc] initWithFrame:CGRectMake(0, bgView.maxY + KScaleWidth(8.0), self.width, KScaleWidth(78.0))];
    defaultBgView.backgroundColor = kColorHEX(0xffffff);
    [_contentView addSubview:defaultBgView];
    {
        UILabel *defaultTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(25.0), KScaleWidth(13.0), self.width - KScaleWidth(25.0), KScaleWidth(28.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
        defaultTitleLabel.text = @"设为默认地址";
        [defaultBgView addSubview:defaultTitleLabel];
        
        UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(defaultTitleLabel.x, defaultTitleLabel.maxY, defaultTitleLabel.width, KScaleWidth(24.0)) font:KFontSize(13.0) textColor:kColorHEX(0xB8B8B8)];
        subTitleLabel.text = @"提示：每次下单/发布见面会默认推荐该地址";
        [defaultBgView addSubview:subTitleLabel];
        [defaultBgView addSubview:self.defaultSwitch];
        _defaultSwitch.x = defaultBgView.width - _defaultSwitch.width - KScaleWidth(18.0);
        _defaultSwitch.y = (defaultBgView.height - _defaultSwitch.height)/2;
    }
    
    UIButton *saveBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(17.0) frame:CGRectMake((self.width - KScaleWidth(320.0))/2, _contentView.height - KScaleWidth(80.0) - KScaleWidth(44.0), KScaleWidth(320.0), KScaleWidth(44.0)) title:@"保存" titleColor:kColorHEX(0x333333)];
    [saveBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
    [saveBtn setCornerRadius:saveBtn.height/2];
    [saveBtn addTarget:self action:@selector(saveAction:) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:saveBtn];
    
    self.bgView.alpha = 0;
}

//地区选择结果
- (void)setAreaSelectResult:(NSString *)areaStr areaId:(NSString *)areaId {
    [_placeSelectBtn setTitle:areaStr forState:UIControlStateNormal];
    _areaStr = areaStr;
    _areaId = areaId;
    _addressModel.areaId = areaId;
    _addressModel.areaName = areaStr;
    [_placeSelectBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateNormal];
    [_placeSelectBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleRight ImageTitleSpace:KScaleWidth(8.0)];
}

//地址添加成功
- (void)addressAddSuccess {
    _nameTextField.text = @"";
    _phoneTextField.text = @"";
    _addressTextField.text = @"";
    [_placeSelectBtn setTitle:@"省、市、区" forState:UIControlStateNormal];
    [_placeSelectBtn setTitleColor:kColorHEX(0x999999) forState:UIControlStateNormal];
    [_placeSelectBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleRight ImageTitleSpace:KScaleWidth(8.0)];
}

//地址选择
- (void)placeSelectAction:(UIButton *)button {
    self.areaSelectBlock ? self.areaSelectBlock() : nil;
}

//保存
- (void)saveAction:(UIButton *)button {
    if (_nameTextField.text.length == 0) {
        [MBProgressHUD showError:@"请输入见面人姓名"];
        return;
    }
    if (_phoneTextField.text.length == 0) {
        [MBProgressHUD showError:@"请输入手机号码"];
        return;
    }
    if ([_placeSelectBtn.titleLabel.text isEqualToString:@"省、市、区"]) {
        [MBProgressHUD showError:@"请选择所在地区"];
        return;
    }
    if (_addressTextField.text.length == 0) {
        [MBProgressHUD showError:@"请输入详细地址"];
        return;
    }
    _addressModel.name = _nameTextField.text;
    _addressModel.mobile = _phoneTextField.text;
    _addressModel.detailAddress = _addressTextField.text;
    _addressModel.areaName = _placeSelectBtn.titleLabel.text;
    _addressModel.areaId = _areaId;
    _addressModel.defaultStatus = _defaultSwitch.on;
    
    self.addAddressBlock ? self.addAddressBlock(_addressModel) : nil;
    [self disMiss];
}

//弹出
- (void)showWithAddressModel:(XMAddressModel *)addressModel {
    if (!addressModel) {
        addressModel = [[XMAddressModel alloc] init];
        addressModel.isCreate = true;
    }
    _addressModel = addressModel;
    _nameTextField.text = addressModel.name;
    _phoneTextField.text = addressModel.mobile;
    _areaId = addressModel.areaId;
    if (addressModel.areaName && addressModel.areaName.length > 0) {
        [_placeSelectBtn setTitle:addressModel.areaName forState:UIControlStateNormal];
        [_placeSelectBtn setTitleColor:kColorHEX(0x333333) forState:UIControlStateNormal];
        [_placeSelectBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleRight ImageTitleSpace:KScaleWidth(8.0)];
    }
    else {
        [_placeSelectBtn setTitle:@"省、市、区" forState:UIControlStateNormal];
        [_placeSelectBtn setTitleColor:kColorHEX(0x999999) forState:UIControlStateNormal];
        [_placeSelectBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleRight ImageTitleSpace:KScaleWidth(8.0)];
    }
    _addressTextField.text = addressModel.detailAddress;
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.bgView.alpha = 1;
        weakSelf.contentView.y = weakSelf.height - weakSelf.contentView.height;
    }];
}

- (void)disMiss {
    XMWeakSelf;
    [UIView animateWithDuration:.3 animations:^{
        weakSelf.bgView.alpha = 0;
        weakSelf.contentView.y = weakSelf.height;
    } completion:^(BOOL finished) {
        [weakSelf removeFromSuperview];
    }];
}

#pragma mark - Init

- (UIView *)bgView {
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:self.bounds];
        _bgView.backgroundColor = kColorHEXA(0x464646, 0.46);
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(disMiss)];
        [_bgView addGestureRecognizer:tap];
    }
    return _bgView;
}

- (UITextField *)nameTextField {
    if (!_nameTextField) {
        _nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.width/3, KScaleWidth(10.0), self.width/3*2 - KScaleWidth(24.), KScaleWidth(50.0))];
        _nameTextField.font = KFontSize(16.0);
        _nameTextField.textAlignment = NSTextAlignmentRight;
        _nameTextField.placeholder = @"请填写见面人的姓名";
    }
    return _nameTextField;
}

- (UITextField *)phoneTextField {
    if (!_phoneTextField) {
        _phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.nameTextField.x, _nameTextField.maxY, _nameTextField.width, _nameTextField.height)];
        _phoneTextField.font = KFontSize(16.0);
        _phoneTextField.textAlignment = NSTextAlignmentRight;
        _phoneTextField.placeholder = @"请填写手机号码";
    }
    return _phoneTextField;
}

- (UIButton *)placeSelectBtn {
    if (!_placeSelectBtn) {
//        _placeSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(self.width - KScaleWidth(17.0) - KScaleWidth(115.0), self.phoneTextField.maxY, KScaleWidth(115.0), _phoneTextField.height) title:@"省、市、区" titleColor:kColorHEX(0x999999) image:@"next_icon_black"];
        _placeSelectBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(16.0) frame:CGRectMake(self.phoneTextField.x, _phoneTextField.maxY, _phoneTextField.width, _phoneTextField.height) title:@"省、市、区" titleColor:kColorHEX(0x999999) image:@"next_icon_black"];
        _placeSelectBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_placeSelectBtn layoutButtonWithEdgeInsetsStyle:CSButtonEdgeInsetsStyleRight ImageTitleSpace:KScaleWidth(8.0)];
        [_placeSelectBtn addTarget:self action:@selector(placeSelectAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _placeSelectBtn;
}

- (UITextField *)addressTextField {
    if (!_addressTextField) {
        _addressTextField = [[UITextField alloc] initWithFrame:CGRectMake(self.phoneTextField.x, self.placeSelectBtn.maxY, _phoneTextField.width, _phoneTextField.height)];
        _addressTextField.font = KFontSize(16.0);
        _addressTextField.textAlignment = NSTextAlignmentRight;
        _addressTextField.placeholder = @"街道/楼牌号";
    }
    return _addressTextField;;
}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.height, self.width, self.height - KScaleWidth(165.0))];
        _contentView.backgroundColor = kColorHEX(0xF5F5F5);
        [_contentView setPartCornerWithRectCorner:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadius:KScaleWidth(12.0)];
    }
    return _contentView;
}

- (UISwitch *)defaultSwitch {
    if (!_defaultSwitch) {
        _defaultSwitch = [[UISwitch alloc] init];
        [_defaultSwitch setTintColor:kColorHEX(0x999999)];
        [_defaultSwitch setOnTintColor:kColorHEX(0xFFD800)];
    }
    return _defaultSwitch;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
